﻿using AdminSystem.Domain;
using AdminSystem.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminSystem.Repositories
{
    public class AdminRepository :Repository<Admin>, IAdminRepository
    {
        private readonly AdminSystemDbContext db;

        public AdminRepository(AdminSystemDbContext db) : base(db)
        {
            this.db = db;
        }


        /// <summary>
        /// 查全部
        /// </summary>
        /// <returns></returns>
        public async Task<List<Admin>> GetAllAsync()
        {
            return await db.Admins.ToListAsync();
        }

        /// <summary>
        /// 重写基类的方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public override async Task<Admin> InsertAsync(Admin entity)
        {
            //重名判断
            var user = await db.Admins.FirstOrDefaultAsync(m=>m.UserName == entity.UserName);
            if (user != null) 
            {
                throw new Exception("重名了");
            }

            return await base.InsertAsync(entity);
        }
    }
}
